﻿/*  
    Copyright (C) <2007-2013>  <Kay Diefenthal>

    ArgusTv.UI.MediaCenter is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    ArgusTv.UI.MediaCenter is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with ArgusTv.UI.MediaCenter.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using ArgusTV.DataContracts;
using ArgusTV.ServiceAgents;
using ArgusTV.UI.MediaCenter.Logging;
using ArgusTV.UI.MediaCenter.Models;
using ArgusTV.UI.MediaCenter.Properties;
using ArgusTV.UI.MediaCenter.Views;
using Microsoft.MediaCenter;
using Microsoft.MediaCenter.Hosting;
using Microsoft.MediaCenter.UI;

namespace ArgusTV.UI.MediaCenter.ViewModels
{
    public class RecordingModel : ModelItem
    {
        #region Private Fields

        private readonly ChannelType _channelType;
        private readonly AddInHost _host;
        private readonly HistoryOrientedPageSession _session;

        #endregion

        #region Constructor

        /// <summary>
        /// Initialize a new Instance of <see cref="RecordingModel"/> Class.
        /// </summary>
        /// <param name="session"></param>
        /// <param name="host"></param>
        /// <param name="type"></param>
        public RecordingModel(HistoryOrientedPageSession session, AddInHost host, Int32 type)
        {
            _session = session;
            _host = host;
            _channelType = (ChannelType) type;
        }

        #endregion

        #region Properties

        /// <summary>
        /// MediaCenterEnvironment
        /// </summary>
        public MediaCenterEnvironment MediaCenterEnvironment
        {
            get
            {
                if (_host == null) return null;
                return _host.MediaCenterEnvironment;
            }
        }
        
        #endregion

        #region RecordedView
        /// <summary>
        /// Initialize a new Instance of <see cref="RecordedView"/> Class.
        /// </summary>
        /// <returns></returns>
        private RecordedView CreateRecordedView()
        {
            try
            {
                var view = new RecordedView();
                view.AddCommand = new Command(view, Resources.ScheduleAddOptionsDescription, null);
                view.ViewCommand = new Command(view, Resources.ScheduledViewDescription, null);
                var contentList = new VirtualList(view, null) {EnableSlowDataRequests = true};
                view.Content = contentList;
                CreateRecordedViewModeFilters(view);

                view.AddCommandInvoked += delegate
                    {
                        GoToScheduleOptionsView(CreateScheduleOptionsView());
                    };
                view.ViewCommandInvoked += delegate
                    {
                        GoToScheduledView(CreateScheduleView()); 
                    };

                return view;
            }
            catch (Exception exception)
            {
                Logger.Error(string.Format("{0}{1}:{2}", "RecordingViewModel", "CreateLiveView", exception));
                return null;
            }
        }

        /// <summary>
        /// CreateRecordedViewModeFilters
        /// </summary>
        /// <param name="view"></param>
        private void CreateRecordedViewModeFilters(RecordedView view)
        {
            try
            {
                var list = new ArrayListDataSet();
                ModelItem one = new RecordingGroupModeFilter(view, Resources.SortByProgramTitleDescription,
                                                             RecordingGroupMode.GroupByProgramTitle);
                list.Add(one);
                ModelItem two = new RecordingGroupModeFilter(view, Resources.SortByScheduleDescription,
                                                             RecordingGroupMode.GroupBySchedule);
                list.Add(two);
                ModelItem three = new RecordingGroupModeFilter(view, Resources.SortByCategoryDescription,
                                                               RecordingGroupMode.GroupByCategory);
                list.Add(three);
                ModelItem four = new RecordingGroupModeFilter(view, Resources.SortByChannelDescription,
                                                              RecordingGroupMode.GroupByChannel);
                list.Add(four);
                ModelItem five = new RecordingGroupModeFilter(view, Resources.SortByDateDescription,
                                                              RecordingGroupMode.GroupByRecordingDay);
                list.Add(five);
                view.RecordedModeFilterChanged += delegate(object sender, EventArgs e)
                    {
                        var recordedview = (RecordedView) sender;
                        var selected = (RecordingGroupModeFilter) recordedview.RecordedModeFilters.Chosen;
                        CreateRecordedViewGroupFilters(recordedview, selected.RecordingGroupMode);
                    };
                view.RecordedModeFilters = new Choice(view, null, list);
            }
            catch (Exception exception)
            {
                Logger.Error(string.Format("{0}{1}:{2}", "RecordingViewModel", "CreateRecordedViewModeFilters",
                                           exception));
            }

        }

        /// <summary>
        /// CreateRecordedViewGroupFilters
        /// </summary>
        /// <param name="view"></param>
        /// <param name="recordingGroupMode"></param>
        private void CreateRecordedViewGroupFilters(RecordedView view, RecordingGroupMode recordingGroupMode)
        {
            try
            {
                var list = new ArrayListDataSet();
                switch (recordingGroupMode)
                {
                    case RecordingGroupMode.GroupByProgramTitle:
                        using (var controlServiceAgent = new ControlServiceAgent())
                        {
                            var groups = controlServiceAgent.GetAllRecordingGroups(_channelType,RecordingGroupMode.GroupByProgramTitle);
                            if (groups.Length == 0)
                            {
                                view.RecordedGroupFilters = new Choice(view, null, list);
                            }
                            else
                            {
                                foreach (
                                    var item in groups.Select(t => new RecordingGroupFilter(view, t.ProgramTitle, t)))
                                {
                                    list.Add(item);
                                }
                                view.RecordedGroupFilters = new Choice(view, null, list);
                            }
                        }
                        break;
                    case RecordingGroupMode.GroupBySchedule:
                        using (var controlServiceAgent = new ControlServiceAgent())
                        {
                            var groups = controlServiceAgent.GetAllRecordingGroups(
                                _channelType,
                                RecordingGroupMode
                                    .GroupBySchedule);
                            if (groups.Length == 0)
                            {
                                view.RecordedGroupFilters = new Choice(view, null, list);
                            }
                            else
                            {
                                foreach (
                                    var item in
                                        groups.Select(t => new RecordingGroupFilter(view, t.ScheduleName, t))
                                    )
                                {
                                    list.Add(item);
                                }
                                view.RecordedGroupFilters = new Choice(view, null, list);
                            }
                        }
                        break;
                    case RecordingGroupMode.GroupByCategory:
                        using (var controlServiceAgent = new ControlServiceAgent())
                        {
                            var groups = controlServiceAgent.GetAllRecordingGroups(
                                _channelType,
                                RecordingGroupMode
                                    .GroupByCategory);
                            if (groups.Length == 0)
                            {
                                view.RecordedGroupFilters = new Choice(view, null, list);
                            }
                            else
                            {
                                foreach (
                                    var item in
                                        groups.Select(t => new RecordingGroupFilter(view, t.Category, t)))
                                {
                                    list.Add(item);
                                }
                                view.RecordedGroupFilters = new Choice(view, null, list);
                            }
                        }
                        break;
                    case RecordingGroupMode.GroupByChannel:
                        using (var controlServiceAgent = new ControlServiceAgent())
                        {
                            var groups = controlServiceAgent.GetAllRecordingGroups(_channelType,
                                                                                                       RecordingGroupMode
                                                                                                           .GroupByChannel);
                            if (groups.Length == 0)
                            {
                                view.RecordedGroupFilters = new Choice(view, null, list);
                            }
                            else
                            {
                                foreach (
                                    var item in
                                        groups.Select(
                                            t => new RecordingGroupFilter(view, t.ChannelDisplayName, t)))
                                {
                                    list.Add(item);
                                }
                                view.RecordedGroupFilters = new Choice(view, null, list);
                            }
                        }
                        break;
                    case RecordingGroupMode.GroupByRecordingDay:
                        using (var controlServiceAgent = new ControlServiceAgent())
                        {
                            var groups = controlServiceAgent.GetAllRecordingGroups(_channelType,
                                                                                                   RecordingGroupMode
                                                                                                       .GroupByRecordingDay);
                            if (groups.Length == 0)
                            {
                                view.RecordedGroupFilters = new Choice(view, null, list);
                            }
                            else
                            {
                                foreach (
                                    var item in
                                        groups.Select(
                                            t =>
                                            new RecordingGroupFilter(view,
                                                                     t.LatestProgramStartTime.ToShortDateString(),
                                                                     t)))
                                {
                                    list.Add(item);
                                }
                                view.RecordedGroupFilters = new Choice(view, null, list);
                            }
                        }
                        break;
                
                }
                if (list.Count > 0)
                {
                    view.RecordedGroupFilterChanged += delegate(object sender, EventArgs e)
                        {
                            var recordedview = (RecordedView) sender;
                            var selected = (RecordingGroupFilter) recordedview.RecordedGroupFilters.Chosen;
                            CreateRecordedViewContent(recordedview, selected);
                        };
                }
            }
            catch (Exception exception)
            {
                Logger.Error(string.Format("{0}{1}:{2}", "RecordingViewModel", "CreateRecordedViewGroupFilters",exception));
            }
        }

        /// <summary>
        /// CreateRecordedViewContent
        /// </summary>
        /// <param name="view"></param>
        /// <param name="filter"></param>
        private void CreateRecordedViewContent(RecordedView view, RecordingGroupFilter filter)
        {
            try
            {
                view.Content.Clear();
                
                switch (filter.RecordingGroup.RecordingGroupMode)
                {
                    case RecordingGroupMode.GroupByProgramTitle:
                        using (var controlServiceAgent = new ControlServiceAgent())
                        {
                            var summaries = controlServiceAgent.GetRecordingsForProgramTitle(_channelType,
                                                                                             filter.RecordingGroup
                                                                                                   .ProgramTitle, false);
                            foreach (var t in summaries.Where(t => summaries.Length > 0))
                            {
                                view.Content.Add(CreateRecordingCommand(t));
                            }
                        }
                        break;
                    case RecordingGroupMode.GroupBySchedule:
                        using (var controlServiceAgent = new ControlServiceAgent())
                        {
                            var summaries = controlServiceAgent.GetRecordingsForSchedule(filter.RecordingGroup.ScheduleId, false);
                            foreach (var t in summaries.Where(t => summaries.Length > 0))
                            {
                                view.Content.Add(CreateRecordingCommand(t));
                            }
                        }
                        break;
                    case RecordingGroupMode.GroupByCategory:
                        using (var controlServiceAgent = new ControlServiceAgent())
                        {
                            var summaries = controlServiceAgent.GetRecordingsForCategory(_channelType,
                                                                                         filter.RecordingGroup.Category,
                                                                                         false);
                            foreach (var t in summaries.Where(t => summaries.Length > 0))
                            {
                                view.Content.Add(CreateRecordingCommand(t));
                            }
                        }
                        break;
                    case RecordingGroupMode.GroupByChannel:
                        using (var controlServiceAgent = new ControlServiceAgent())
                        {
                            var summaries = controlServiceAgent.GetRecordingsOnChannel(
                                filter.RecordingGroup.ChannelId, false);
                            foreach (var t in summaries.Where(t => summaries.Length > 0))
                            {
                                view.Content.Add(CreateRecordingCommand(t));
                            }
                        }
                        break;
                    case RecordingGroupMode.GroupByRecordingDay:
                        using (var controlServiceAgent = new ControlServiceAgent())
                        {
                            var summaries = controlServiceAgent.GetRecordingsForOneDay(_channelType,
                                                                                       filter.RecordingGroup
                                                                                             .LatestProgramStartTime,
                                                                                       false);
                            foreach (var t in summaries.Where(t => summaries.Length > 0))
                            {
                                view.Content.Add(CreateRecordingCommand(t));
                            }
                        }
                        break;
                }
            }
            catch (Exception exception)
            {
                Logger.Error(string.Format("{0}{1}:{2}", "RecordingViewModel", "CreateRecordedViewContent", exception));
            }
        }

        /// <summary>
        /// Initialize a new Instance of <see cref="RecordingCommand"/> Class.
        /// </summary>
        /// <param name="summay"></param>
        /// <returns></returns>
        private RecordingCommand CreateRecordingCommand(RecordingSummary summay)
        {
            try
            {
                using (var controlServiceAgent = new ControlServiceAgent())
                {
                    var item = new RecordingCommand();
                    var recording = controlServiceAgent.GetRecordingById(summay.RecordingId);
                    item.Description = string.Format("{0}{1}{2}{3}{4}", recording.Title, " - ",
                                                     recording.ChannelDisplayName,
                                                     " - ", recording.ProgramStartTime);
                    item.UniqueId = recording.RecordingId;
                    item.Recording = recording;

                    item.Invoked += delegate(object sender, EventArgs args)
                        {
                            var recordedItem = (RecordingCommand) sender;
                            GoToDetailsView(CreateRecordedDetailsView(recordedItem.Recording));
                        };
                    return item;
                }
            }
            catch (Exception exception)
            {
                Logger.Error(string.Format("{0}{1}:{2}", "RecordingViewModel", "CreateRecordingCommand", exception));
                return null;
            }
        }

        /// <summary>
        /// Initialize a new Instance of <see cref="DetailsView"/> Class.
        /// </summary>
        /// <param name="recording"></param>
        /// <returns></returns>
        private DetailsView CreateRecordedDetailsView(Recording recording)
        {
            var view = new DetailsView
                {
                    Title = recording.Title + " : " + recording.SubTitle,
                    Summary = String.Format("{0}{1}{2}{3}{4}{5}{6}", recording.ChannelDisplayName, ", ",
                                            recording.StartTime.ToShortDateString(), " ",
                                            recording.StartTime.ToShortTimeString(), " - ",
                                            recording.StopTime.ToShortTimeString()),
                    Metadata = recording.Description
                };
            view.Commands = new ArrayListDataSet(view);
            CreateRecordedDetailCommands(view, Resources.PlayDescription, DetailsCommandTypes.Play, recording,
                                         "res://ehres!DETAILS.CMD.PLAY.DORMANT.PNG",
                                         "res://ehres!DETAILS.CMD.PLAY.FOCUS.PNG", "res://ehres!DETAILS.CMD.PLAY.PNG");
            CreateRecordedDetailCommands(view, Resources.DeleteDescription, DetailsCommandTypes.Delete, recording,
                                         "res://ehres!DETAILS.CMD.REMOVE.DORMANT.PNG",
                                         "res://ehres!DETAILS.CMD.REMOVE.FOCUS.PNG",
                                         "res://ehres!DETAILS.CMD.REMOVE.PNG");

            return view;
        }

        /// <summary>
        /// CreateRecordedDetailCommands
        /// </summary>
        /// <param name="view"></param>
        /// <param name="description"></param>
        /// <param name="type"></param>
        /// <param name="recording"></param>
        /// <param name="dormant"></param>
        /// <param name="focus"></param>
        /// <param name="nofocus"></param>
        private void CreateRecordedDetailCommands(DetailsView view, String description, DetailsCommandTypes type,Recording recording, String dormant, String focus, String nofocus)
        {
            var command = new DetailsCommand(view, description, type, recording)
                {
                    Dormant = new Image(dormant),
                    Focus = new Image(focus),
                    NoFocus = new Image(nofocus)
                };
            command.Invoked += (sender, e) => OnDetailCommandInvoked((DetailsCommand) sender, recording);
            view.Commands.Add(command);
        }

        /// <summary>
        /// OnDetailCommandInvoked
        /// </summary>
        /// <param name="command"></param>
        /// <param name="recording"></param>
        private void OnDetailCommandInvoked(DetailsCommand command, Recording recording)
        {
            switch (command.Type)
            {
                case DetailsCommandTypes.Play:
                    using (var controlServiceAgent = new ControlServiceAgent())
                    {
                        var url = controlServiceAgent.StartRecordingStream(recording.RecordingFileName);
                        var urlitems = url.Split(new[] {':', '/', '/', ':', '/'});
                        var protocol = urlitems[0];
                        var port = urlitems[4];
                        var media = urlitems[5];
                        var ipaddress = Settings.Default.ServerIpAddress;
                        var newurl = Utils.RebuildMediaStreamUrl(protocol, ipaddress, port, media);
                        AddInHost.Current.MediaCenterEnvironment.PlayMedia(MediaType.Video, newurl, false);
                    }
                    _session.BackPage();
                    break;
                case DetailsCommandTypes.Delete:
                    MediaCenterEnvironment.Dialog(Resources.DeleteInfoDescription, Resources.DeleteCaptionDescription,
                                                  new object[] {DialogButtons.Yes, DialogButtons.No}, 0, true, null,
                                                  delegate(DialogResult result)
                                                      {
                                                          if (result == DialogResult.Yes)
                                                          {
                                                              using (var controlServiceAgent = new ControlServiceAgent())
                                                              {
                                                                  controlServiceAgent.DeleteRecording(
                                                                      recording.RecordingFileName, true);
                                                                  _session.BackPage();
                                                              }
                                                          }
                                                          if (result == DialogResult.No)
                                                          {
                                                              _session.BackPage();
                                                          }
                                                      });
                    break;
            }
        }

        /// <summary>
        /// Navigate to DetailsView
        /// </summary>
        /// <param name="view"></param>
        private void GoToDetailsView(DetailsView view)
        {
            try
            {
                var uiProperties = new Dictionary<string, object>();
                uiProperties["DetailsView"] = view;
                _session.GoToPage("resx://ArgusTV.UI.MediaCenter/ArgusTV.UI.MediaCenter.Resources/DetailsView",
                                  uiProperties);
            }
            catch (Exception exception)
            {
                Logger.Error(string.Format("{0}{1}:{2}", "RecordingViewModel", "GoToDetailsView", exception));
            }
        }

        /// <summary>
        /// Navigate to RecordingView
        /// </summary>
        public void GoToRecordingView()
        {
            try
            {
                var uiProperties = new Dictionary<string, object>();
                uiProperties["RecordedView"] = CreateRecordedView();
                _session.GoToPage("resx://ArgusTV.UI.MediaCenter/ArgusTV.UI.MediaCenter.Resources/RecordedView",
                                  uiProperties);
            }
            catch (Exception exception)
            {
                Logger.Error(string.Format("{0}{1}:{2}", "RecordingViewModel", "GoToRecordingView", exception));
            }
        }

        #endregion

        #region ScheduleOptionsView

        /// <summary>
        /// Initialize a new Instance of <see cref="ScheduleOptionsView"/> Class.
        /// </summary>
        /// <returns></returns>
        private ScheduleOptionsView CreateScheduleOptionsView()
        {
            var view = new ScheduleOptionsView
                {
                    GuideCommand = new Command(null, "Guide", OnGuideInvoked),
                    SearchCommand = new Command(null, "Search", OnSearchInvoked),
                    ChannelAndTimeCommand = new Command(null, "Channel and Time ", OnChannelAndTimeInvoked)
                };
            return view;
        }

        /// <summary>
        /// Navigate to ScheduleOptionsView
        /// </summary>
        /// <param name="scheduleOptionsView"></param>
        private void GoToScheduleOptionsView(ScheduleOptionsView scheduleOptionsView)
        {
            var uiProperties = new Dictionary<string, object>();
            uiProperties["ScheduleOptionsView"] = scheduleOptionsView;
            _session.GoToPage("resx://ArgusTV.UI.MediaCenter/ArgusTV.UI.MediaCenter.Resources/ScheduleOptionsView", uiProperties);
        }

        /// <summary>
        /// OnChannelAndTimeInvoked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnChannelAndTimeInvoked(object sender, EventArgs e)
        {
            GoToScheduleManualView(CreateScheduleManualView());
        }

        /// <summary>
        /// OnSearchInvoked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnSearchInvoked(object sender, EventArgs e)
        {
            int type = 0;
            switch (_channelType)
            {
                case ChannelType.Radio:
                    type = 1;
                    break;
                case ChannelType.Television:
                    type = 0;
                    break;
            }
            var model = new SearchModel(_session,_host,type);
            model.GoToSearchOptionsView();
        }

        /// <summary>
        /// OnGuideInvoked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnGuideInvoked(object sender, EventArgs e)
        {
            int type = 0;
            switch (_channelType)
            {
                 case ChannelType.Radio:
                    type = 1;
                    break;
                 case ChannelType.Television:
                    type = 0;
                    break;
            }
            var model = new EpgModel(_session,_host,type);
            model.GoToMultiChannelEpgView();
        }

        #endregion

        #region ScheduledView

        /// <summary>
        /// Initialize a new Instance of <see cref="ScheduledView"/>Class.
        /// </summary>
        /// <returns></returns>
        private ScheduledView CreateScheduleView()
        {
            try
            {
                var view = new ScheduledView();
                var contentList = new VirtualList(view, null) { EnableSlowDataRequests = true };
                view.Content = contentList;
                CreateScheduleViewFilters(view);
                return view;
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// CreateScheduleViewFilters
        /// </summary>
        /// <param name="view"></param>
        private void CreateScheduleViewFilters(ScheduledView view)
        {
            try
            {
                var list = new ArrayListDataSet(view);
                ModelItem recording = new ScheduleTypeFilter(view, Resources.SortByRecordingDescription, ScheduleType.Recording);
                list.Add(recording);
                list.Add(new ScheduleTypeFilter(view, Resources.SortByAlertDescription, ScheduleType.Alert));
                list.Add(new ScheduleTypeFilter(view, Resources.SortBySuggestionDescription, ScheduleType.Suggestion));
                var filters = new Choice(view, null, list) { Chosen = recording };
                view.FilterChanged += delegate(object sender, EventArgs args)
                {
                    var scheduledView = (ScheduledView)sender;
                    var selected = (ScheduleTypeFilter)scheduledView.Filters.Chosen;
                    CreateScheduleViewContent(scheduledView, selected);
                };
                view.Filters = filters;
            }
            catch(Exception exception)
            {
                Console.WriteLine(exception);
            }
        }

        /// <summary>
        /// CreateScheduleViewContent
        /// </summary>
        /// <param name="view"></param>
        /// <param name="filter"></param>
        private void CreateScheduleViewContent(ScheduledView view, ScheduleTypeFilter filter)
        {
            try
            {
                
                view.Content.Clear();
                switch (filter.ScheduleType)
                {
                    case ScheduleType.Recording:
                        using (var schedulerServiceAgent = new SchedulerServiceAgent())
                        {
                            var summaries = schedulerServiceAgent.GetAllSchedules(_channelType, ScheduleType.Recording, true);
                            if (summaries.Length > 0)
                            {
                                foreach (var t in summaries)
                                {
                                    view.Content.Add(CreateScheduleCommand(t));
                                }
                            }
                        }
                        break;
                    case ScheduleType.Alert:
                        using (var schedulerServiceAgent = new SchedulerServiceAgent())
                        {
                            var summaries = schedulerServiceAgent.GetAllSchedules(_channelType, ScheduleType.Alert, true);
                            if (summaries.Length > 0)
                            {
                                foreach (var t in summaries)
                                {
                                    view.Content.Add(CreateScheduleCommand(t));
                                }
                            }
                        }
                        break;
                    case ScheduleType.Suggestion:
                        using (var schedulerServiceAgent = new SchedulerServiceAgent())
                        {
                            var summaries = schedulerServiceAgent.GetAllSchedules(_channelType, ScheduleType.Suggestion,
                                                                                  true);
                            if (summaries.Length > 0)
                            {
                                foreach (var t in summaries)
                                {
                                    view.Content.Add(CreateScheduleCommand(t));
                                }
                            }
                        }
                        break;
                }
            }
            catch (Exception exception)
            {
                Logger.Error(string.Format("{0}{1}:{2}", "RecordingViewModel", " CreateScheduleViewContent", exception));
            }
        }

        /// <summary>
        /// Initialize a new Instance of <see cref="ScheduleCommand"/> Class.
        /// </summary>
        /// <param name="summary"></param>
        /// <returns></returns>
        private ScheduleCommand CreateScheduleCommand(ScheduleSummary summary)
        {
            try
            {
                using (var schedulerServiceAgent = new SchedulerServiceAgent())
                {
                    var item = new ScheduleCommand();
                    Schedule schedule = schedulerServiceAgent.GetScheduleById(summary.ScheduleId);
                    item.Description = schedule.Name;
                    item.UniqueId = schedule.ScheduleId;
                    item.Schedule = schedule;
                    item.Invoked += delegate(object sender, EventArgs args)
                        {
                            var scheduledItem = (ScheduleCommand) sender;
                            var scheduleEditView = CreateScheduleEditView(scheduledItem.Schedule);
                            GoToScheduleEditView(scheduleEditView);
                        };
                    return item;
                }
            }
            catch (Exception exception)
            {
                Logger.Error(string.Format("{0}{1}:{2}", "RecordingViewModel", " CreateScheduleCommand", exception));
                return null;
            }
        }

        /// <summary>
        /// Initialize a new Instance of <see cref="ScheduleEditView"/> Class.
        /// </summary>
        /// <param name="schedule"></param>
        /// <returns></returns>
        private ScheduleEditView CreateScheduleEditView(Schedule schedule)
        {
            try
            {
                var view = new ScheduleEditView();
                view.Commands = new ArrayListDataSet(view);
                CreateScheduleEditCommand(view, Resources.SaveDescription, ScheduleEditCommandTypes.Save, schedule);
                CreateScheduleEditCommand(view, Resources.DeleteDescription, ScheduleEditCommandTypes.Delete, schedule);
                CreateScheduleEditCommand(view, Resources.CancelDescription, ScheduleEditCommandTypes.Cancel, schedule);
                view.ScheduleName = new EditableText(view) {Value = schedule.Name};
                CreateKeepUntilModeFilters(view);
                CreateSchedulePriorityFilters(view);
                CreateScheduleTypeFilters(view);
                return view;
            }
            catch (Exception exception)
            {
                Logger.Error(string.Format("{0}{1}:{2}", "RecordingViewModel", " CreateScheduleEditView", exception));
                return null;
            }
        }

        /// <summary>
        /// CreateScheduleEditCommand
        /// </summary>
        /// <param name="view"></param>
        /// <param name="description"></param>
        /// <param name="type"></param>
        /// <param name="schedule"></param>
        private void CreateScheduleEditCommand(ScheduleEditView view, String description, ScheduleEditCommandTypes type,
                                               Schedule schedule)
        {
            try
            {
                var command = new ScheduleEditCommand(view, description, type, schedule);

                command.Invoked += (sender, e) => OnScheduleEditCommandInvoked((ScheduleEditCommand)sender, schedule, view);
                view.Commands.Add(command);
            }
            catch (Exception exception)
            {
                Logger.Error(string.Format("{0}{1}:{2}", "RecordingViewModel", "CreateScheduleEditCommand", exception));
            }
        }

        /// <summary>
        /// OnScheduleEditCommandInvoked
        /// </summary>
        /// <param name="command"></param>
        /// <param name="schedule"></param>
        /// <param name="view"></param>
        private void OnScheduleEditCommandInvoked(ScheduleEditCommand command, Schedule schedule, ScheduleEditView view)
        {
            try
            {
                switch (command.Type)
                {
                    case ScheduleEditCommandTypes.Save:
                        MediaCenterEnvironment.Dialog(Resources.SaveInfoDescription, Resources.SaveCaptionDescription,
                                                      new object[] { DialogButtons.Yes, DialogButtons.No }, 10, true, null,
                                                      delegate(DialogResult result)
                                                      {
                                                          if (result == DialogResult.Yes)
                                                          {
                                                              using (var schedulerServiceAgent =new SchedulerServiceAgent())
                                                              {
                                                                  schedule.Name = view.ScheduleName.Value;
                                                                  schedule.KeepUntilMode =
                                                                      ((KeepUntilModeFilter)
                                                                       view.KeepUntilModeFilters.Chosen).KeepUntilMode;
                                                                  schedule.SchedulePriority =
                                                                      ((SchedulePriorityFilter)
                                                                       view.SchedulePriorityFilters.Chosen)
                                                                          .SchedulePriority;
                                                                  schedule.ScheduleType =
                                                                      ((ScheduleTypeFilter)
                                                                       view.ScheduleTypeFilters.Chosen).ScheduleType;
                                                                  schedulerServiceAgent.SaveSchedule(schedule);
                                                                  _session.BackPage();
                                                              }
                                                          }

                                                          if (result == DialogResult.No)
                                                          {
                                                              _session.BackPage();
                                                          }
                                                      });

                        break;
                    case ScheduleEditCommandTypes.Delete:
                        MediaCenterEnvironment.Dialog(Resources.DeleteInfoDescription, Resources.DeleteCaptionDescription,
                                                      new object[] { DialogButtons.Yes, DialogButtons.No }, 10, true, null,
                                                      delegate(DialogResult result)
                                                          {
                                                              if (result == DialogResult.Yes)
                                                              {
                                                                  using (
                                                                      var schedulerServiceAgent =
                                                                          new SchedulerServiceAgent())
                                                                  {
                                                                      schedulerServiceAgent.DeleteSchedule(
                                                                          schedule.ScheduleId);
                                                                      _session.BackPage();
                                                                  }
                                                              }
                                                              if (result == DialogResult.No)
                                                              {
                                                                  _session.BackPage();
                                                              }
                                                          });

                        break;
                    case ScheduleEditCommandTypes.Cancel:
                        _session.BackPage();
                        break;
                }
            }
            catch (Exception exception)
            {
                Logger.Error(string.Format("{0}{1}:{2}", "RecordingViewModel", "OnScheduleEditCommandInvoked", exception));
            }
        }

        /// <summary>
        /// CreateSchedulePriorityFilters
        /// </summary>
        /// <param name="view"></param>
        private void CreateSchedulePriorityFilters(ScheduleEditView view)
        {
            try
            {
                var list = new ArrayListDataSet(view)
                {
                    new SchedulePriorityFilter(view, Resources.PriorityVeryLowDescription, SchedulePriority.VeryLow),
                    new SchedulePriorityFilter(view, Resources.PriorityLowDescription, SchedulePriority.Low)
                };
                ModelItem normal = new SchedulePriorityFilter(view, Resources.PriorityNormalDescription,SchedulePriority.Normal);
                list.Add(normal);
                list.Add(new SchedulePriorityFilter(view, Resources.PriorityHighDescription, SchedulePriority.High));
                list.Add(new SchedulePriorityFilter(view, Resources.PriorityVeryHighDescription, SchedulePriority.VeryHigh));
                var filters = new Choice(view, null, list) { Chosen = normal };
                view.SchedulePriorityFilters = filters;
            }
            catch (Exception exception)
            {
                Logger.Error(string.Format("{0}{1}:{2}", "RecordingViewModel", "CreateSchedulePriorityFilters", exception));
            }
        }

        /// <summary>
        /// CreateScheduleTypeFilters
        /// </summary>
        /// <param name="view"></param>
        private void CreateScheduleTypeFilters(ScheduleEditView view)
        {
            try
            {
                var list = new ArrayListDataSet(view);
                ModelItem one = new ScheduleTypeFilter(view, Resources.SortByRecordingDescription, ScheduleType.Recording);
                list.Add(one);
                ModelItem two = new ScheduleTypeFilter(view, Resources.SortByAlertDescription, ScheduleType.Alert);
                list.Add(two);
                ModelItem three = new ScheduleTypeFilter(view, Resources.SortBySuggestionDescription,
                                                         ScheduleType.Suggestion);
                list.Add(three);
                var filters = new Choice(view, null, list) { Chosen = one };
                view.ScheduleTypeFilters = filters;
            }
            catch (Exception exception)
            {
                Logger.Error(string.Format("{0}{1}:{2}", "RecordingViewModel", "CreateScheduleTypeFilters", exception));
            }
        }

        /// <summary>
        /// CreateKeepUntilModeFilters
        /// </summary>
        /// <param name="view"></param>
        private void CreateKeepUntilModeFilters(ScheduleEditView view)
        {
            try
            {
                var list = new ArrayListDataSet(view);
                ModelItem forever = new KeepUntilModeFilter(view, Resources.KeepUntilModeForeverDescription,
                                                            KeepUntilMode.Forever);
                list.Add(forever);
                list.Add(new KeepUntilModeFilter(view, Resources.KeepUntilModeNumberOfDaysDescription,
                                                 KeepUntilMode.NumberOfDays));
                list.Add(new KeepUntilModeFilter(view, Resources.KeepUntilModeNumberOfEpisodesDescription,
                                                 KeepUntilMode.NumberOfEpisodes));
                list.Add(new KeepUntilModeFilter(view, Resources.KeepUntilModeNumberOfWatchedEpisodesDescription,
                                                 KeepUntilMode.NumberOfWatchedEpisodes));
                list.Add(new KeepUntilModeFilter(view, Resources.KeepUntilModeUntilSpaceIsNeededDescription,
                                                 KeepUntilMode.UntilSpaceIsNeeded));
                var filters = new Choice(view, null, list) { Chosen = forever };
                view.KeepUntilModeFilters = filters;
            }
            catch (Exception exception)
            {
                Logger.Error(string.Format("{0}{1}:{2}", "RecordingViewModel", "CreateKeepUntilModeFilters", exception));
            }
        }

        /// <summary>
        /// Navigate to ScheduledView
        /// </summary>
        /// <param name="scheduleView"></param>
        private void GoToScheduledView(ScheduledView scheduleView)
        {
            try
            {
                var uiProperties = new Dictionary<string, object>();
                uiProperties["ScheduledView"] = scheduleView;
                _session.GoToPage("resx://ArgusTV.UI.MediaCenter/ArgusTV.UI.MediaCenter.Resources/ScheduledView", uiProperties);
            }
            catch (Exception exception)
            {
                Logger.Error(string.Format("{0}{1}:{2}", "RecordingViewModel", "GoToScheduledView", exception));
            }
        }

        /// <summary>
        /// Navigate to ScheduleEditView
        /// </summary>
        /// <param name="view"></param>
        private void GoToScheduleEditView(ScheduleEditView view)
        {
            try
            {
                var uiProperties = new Dictionary<string, object>();
                uiProperties["ScheduleEditView"] = view;
                _session.GoToPage("resx://ArgusTV.UI.MediaCenter/ArgusTV.UI.MediaCenter.Resources/ScheduleEditView", uiProperties);
            }
            catch (Exception exception)
            {
                Logger.Error(string.Format("{0}{1}:{2}", "RecordingViewModel", "GoToScheduleEditView", exception));
            }
        }
        #endregion

        #region ScheduleManualView

        /// <summary>
        /// GetDateRange
        /// </summary>
        /// <param name="startingDate"></param>
        /// <param name="endingDate"></param>
        /// <returns></returns>
        private IEnumerable<DateTime> GetDateRange(DateTime startingDate, DateTime endingDate)
        {
            if (startingDate > endingDate)
            {
                return null;
            }
            var rv = new List<DateTime>();
            var tmpDate = startingDate;
            do
            {
                rv.Add(tmpDate);
                tmpDate = tmpDate.AddDays(1);
            } while (tmpDate <= endingDate);
            return rv;
        }

        /// <summary>
        /// Initialize a new Instance of <see cref="ScheduleManualView"/>
        /// </summary>
        /// <returns></returns>
        private ScheduleManualView CreateScheduleManualView()
        {
            try
            {
                var start = DateTime.Now;
                var stop = start.AddHours(1);
                var view = new ScheduleManualView();
                view.Commands = new ArrayListDataSet(view);
                CreateScheduleManualCommand(view, Resources.SaveDescription, ScheduleManualCommandTypes.Save);
                CreateScheduleManualCommand(view, Resources.CancelDescription, ScheduleManualCommandTypes.Cancel);
                CreateScheduleManualCommand(view, Resources.EditNameDescription, ScheduleManualCommandTypes.EditName);
                CreateScheduleManualViewGroups(view);
                view.ScheduleName = new EditableText(view) { Value = "My Manual Schedule" };
                view.ScheduleStartHour = new EditableText(view) { Value = start.Hour.ToString(CultureInfo.InvariantCulture) };
                view.ScheduleStartMinute = new EditableText(view) { Value = "00" };
                view.ScheduleStopHour = new EditableText(view) { Value = stop.Hour.ToString(CultureInfo.InvariantCulture) };
                view.ScheduleStopMinute = new EditableText(view) { Value = "00" };
                CreateScheduleManualViewIncidence(view);
                CreateScheduleManualViewDates(view);
                CreateSchedulePriorityFilters(view);
                CreateKeepUntilModeFilters(view);
                CreateScheduleTypeFilters(view);
                return view;
            }
            catch (Exception exception)
            {
                Logger.Error(string.Format("{0}{1}:{2}", "RecordingViewModel", "CreateScheduleManualView",exception));
                return null;
            }
        }

        /// <summary>
        /// CreateScheduleManualCommand
        /// </summary>
        /// <param name="view"></param>
        /// <param name="description"></param>
        /// <param name="type"></param>
        private void CreateScheduleManualCommand(ScheduleManualView view, String description, ScheduleManualCommandTypes type)
        {
            try
            {
                var command = new ScheduleManualCommand(view, description, type);
                command.Invoked += (sender, e) => OnScheduleManualCommandInvoked((ScheduleManualCommand)sender, view);
                view.Commands.Add(command);
            }
            catch (Exception exception)
            {
                Logger.Error(string.Format("{0}{1}:{2}", "RecordingViewModel", "CreateScheduleManualCommand",
                                           exception));
            }
            
        }

        /// <summary>
        /// OnScheduleManualCommandInvoked
        /// </summary>
        /// <param name="command"></param>
        /// <param name="view"></param>
        private void OnScheduleManualCommandInvoked(ScheduleManualCommand command, ScheduleManualView view)
        {
            try
            {
                switch (command.Type)
                {
                    case ScheduleManualCommandTypes.Save:
                        MediaCenterEnvironment.Dialog(Resources.SaveInfoDescription, Resources.SaveCaptionDescription,
                                                      new object[] {DialogButtons.Yes, DialogButtons.No}, 10, true, null,
                                                      delegate(DialogResult result)
                                                          {
                                                              if (result == DialogResult.Yes)
                                                              {
                                                                  using (
                                                                      var schedulerServiceAgent =
                                                                          new SchedulerServiceAgent())
                                                                  {
                                                                      var date =
                                                                          ((ModelItem) view.ScheduleDates.Chosen)
                                                                              .Description;
                                                                      var startdate = Convert.ToDateTime(date);
                                                                      var startmonth = startdate.Month;
                                                                      var startyear = startdate.Year;
                                                                      var startday = startdate.Day;
                                                                      var starthour =
                                                                          int.Parse(view.ScheduleStartHour.Value);
                                                                      var startminute =
                                                                          int.Parse(view.ScheduleStartMinute.Value);
                                                                      var start = new DateTime(startyear, startmonth,
                                                                                               startday, starthour,
                                                                                               startminute, 0);
                                                                      var stophour =
                                                                          int.Parse(view.ScheduleStopHour.Value);
                                                                      var stopminute =
                                                                          int.Parse(view.ScheduleStopMinute.Value);
                                                                      var stop = new DateTime(startyear, startmonth,
                                                                                              startday, stophour,
                                                                                              stopminute, 0);
                                                                      var ts = stop.Subtract(start);
                                                                      var schedule =
                                                                          schedulerServiceAgent.CreateNewSchedule(
                                                                              _channelType,
                                                                              ((ScheduleTypeFilter)
                                                                               view.ScheduleTypeFilters.Chosen)
                                                                                  .ScheduleType);
                                                                      schedule.Name = view.ScheduleName.Value;
                                                                      schedule.Rules.Add(ScheduleRuleType.Channels,
                                                                                         ((ModelItem)
                                                                                          view.ScheduleChannelFilters
                                                                                              .Chosen).UniqueId);
                                                                      schedule.Rules.Add(
                                                                          ScheduleRuleType.ManualSchedule, start,
                                                                          new ScheduleTime(ts));
                                                                      schedule.SchedulePriority =
                                                                          ((SchedulePriorityFilter)
                                                                           view.SchedulePriorityFilters.Chosen)
                                                                              .SchedulePriority;
                                                                      schedule.KeepUntilMode =
                                                                          ((KeepUntilModeFilter)
                                                                           view.KeepUntilModeFilters.Chosen)
                                                                              .KeepUntilMode;
                                                                      switch (
                                                                          ((ScheduleDaysOfWeekFilter)
                                                                           view.ScheduleIncidence.Chosen)
                                                                              .ScheduleDaysOfWeek)
                                                                      {
                                                                          case ScheduleDaysOfWeek.None:
                                                                              schedule.Rules.Add(
                                                                                  ScheduleRuleType.DaysOfWeek,
                                                                                  ScheduleDaysOfWeek.None);
                                                                              schedulerServiceAgent.SaveSchedule(schedule);
                                                                              break;
                                                                          case ScheduleDaysOfWeek.Mondays:
                                                                              schedule.Rules.Add(
                                                                                  ScheduleRuleType.DaysOfWeek,
                                                                                  ScheduleDaysOfWeek.Mondays);
                                                                              schedulerServiceAgent.SaveSchedule(schedule);
                                                                              break;
                                                                          case ScheduleDaysOfWeek.Tuesdays:
                                                                              schedule.Rules.Add(
                                                                                  ScheduleRuleType.DaysOfWeek,
                                                                                  ScheduleDaysOfWeek.Tuesdays);
                                                                              schedulerServiceAgent.SaveSchedule(schedule);
                                                                              break;
                                                                          case ScheduleDaysOfWeek.Wednesdays:
                                                                              schedule.Rules.Add(
                                                                                  ScheduleRuleType.DaysOfWeek,
                                                                                  ScheduleDaysOfWeek.Wednesdays);
                                                                              schedulerServiceAgent.SaveSchedule(schedule);
                                                                              break;
                                                                          case ScheduleDaysOfWeek.Thursdays:
                                                                              schedule.Rules.Add(
                                                                                  ScheduleRuleType.DaysOfWeek,
                                                                                  ScheduleDaysOfWeek.Thursdays);
                                                                              schedulerServiceAgent.SaveSchedule(schedule);
                                                                              break;
                                                                          case ScheduleDaysOfWeek.Fridays:
                                                                              schedule.Rules.Add(
                                                                                  ScheduleRuleType.DaysOfWeek,
                                                                                  ScheduleDaysOfWeek.Fridays);
                                                                              schedulerServiceAgent.SaveSchedule(schedule);
                                                                              break;
                                                                          case ScheduleDaysOfWeek.Saturdays:
                                                                              schedule.Rules.Add(
                                                                                  ScheduleRuleType.DaysOfWeek,
                                                                                  ScheduleDaysOfWeek.Saturdays);
                                                                              schedulerServiceAgent.SaveSchedule(schedule);
                                                                              break;
                                                                          case ScheduleDaysOfWeek.Sundays:
                                                                              schedule.Rules.Add(
                                                                                  ScheduleRuleType.DaysOfWeek,
                                                                                  ScheduleDaysOfWeek.Sundays);
                                                                              schedulerServiceAgent.SaveSchedule(schedule);
                                                                              break;
                                                                          case ScheduleDaysOfWeek.Weekends:
                                                                              schedule.Rules.Add(
                                                                                  ScheduleRuleType.DaysOfWeek,
                                                                                  ScheduleDaysOfWeek.Weekends);
                                                                              schedulerServiceAgent.SaveSchedule(schedule);
                                                                              break;
                                                                          case ScheduleDaysOfWeek.WorkingDays:
                                                                              schedule.Rules.Add(
                                                                                  ScheduleRuleType.DaysOfWeek,
                                                                                  ScheduleDaysOfWeek.WorkingDays);
                                                                              schedulerServiceAgent.SaveSchedule(schedule);
                                                                              break;
                                                                      }
                                                                      _session.BackPage();
                                                                  }

                                                              }
                                                                  if (result == DialogResult.No)
                                                                  {
                                                                      _session.BackPage();
                                                                  }
                                                              });

                                                              break;


                                                              case ScheduleManualCommandTypes.Cancel:
                                                              _session.BackPage();
                                                              break;

                                                              case ScheduleManualCommandTypes.EditName:
                                                              var uiProperties = new Dictionary<string, object>();
                                                              uiProperties["Session"] = _session;
                                                              uiProperties["EditableText"] = view.ScheduleName;
                                                              _session.GoToPage(
                                                                  "resx://ArgusTV.UI.MediaCenter/ArgusTV.UI.MediaCenter.Resources/EditNameView",
                                                                  uiProperties);
                                                              break;
                                                          }
                
            }
            catch (Exception exception)
            {
                Logger.Error(string.Format("{0}{1}:{2}", "RecordingViewModel", "OnScheduleManualCommandInvoked",
                                           exception));
            }
        }

        /// <summary>
        /// CreateScheduleManualViewGroups
        /// </summary>
        /// <param name="view"></param>
        private void CreateScheduleManualViewGroups(ScheduleManualView view)
        {
            try
            {
                var list = new ArrayListDataSet();

                using (var schedulerServiceAgent = new SchedulerServiceAgent())
                {
                    var channelgroups = schedulerServiceAgent.GetAllChannelGroups(_channelType, true);
                    if (channelgroups.Length == 0)
                    {
                        var all = new ModelItem(view);
                        switch (_channelType)
                        {
                            case ChannelType.Television:
                                all.Description = Resources.AllChannelsDescription;
                                all.UniqueId = ChannelGroup.AllTvChannelsGroupId;
                                list.Add(all);
                                break;
                            case ChannelType.Radio:
                                all.Description = Resources.AllChannelsDescription;
                                all.UniqueId = ChannelGroup.AllRadioChannelsGroupId;
                                list.Add(all);
                                break;
                        }
                    }
                    else
                    {
                        foreach (var t in channelgroups)
                        {
                            var item = new ModelItem {Description = t.GroupName, UniqueId = t.ChannelGroupId};
                            list.Add(item);
                        }
                    }
                    var groups = new Choice(view, null, list);
                    view.ScheduleGroupFilterChanged += delegate(object sender, EventArgs args)
                    {
                        var manualview = (ScheduleManualView)sender;
                        var selected = (ModelItem)manualview.ScheduleGroupFilters.Chosen;
                        CreateScheduleManualViewChannels(manualview, selected.UniqueId);
                    };
                    view.ScheduleGroupFilters = groups;
                }
                
            }
            catch (Exception exception)
            {
                Logger.Error(string.Format("{0}{1}{2}", "ScheduleManualView", "CreateScheduleManualViewGroups", exception));
            }
        }

        /// <summary>
        /// CreateScheduleManualViewChannels
        /// </summary>
        /// <param name="view"></param>
        /// <param name="groupId"></param>
        private void CreateScheduleManualViewChannels(ScheduleManualView view, Guid groupId)
        {
            try
            {
                using (var schedulerServiceAgent = new SchedulerServiceAgent())
                {
                    var list = new ArrayListDataSet(view);
                    var channels = schedulerServiceAgent.GetChannelsInGroup(groupId, true);
                    foreach (
                        var item in
                            channels.Select(t => new ModelItem {Description = t.DisplayName, UniqueId = t.ChannelId}))
                    {
                        list.Add(item);
                    }
                    view.ScheduleChannelFilters = new Choice(view, null, list);
                }
            }
            catch (Exception exception)
            {
                Logger.Error(string.Format("{0}{1}{2}", "ScheduleManualView", "CreateScheduleManualViewChannels", exception));
            }
        }

        /// <summary>
        /// CreateScheduleManualViewIncidence
        /// </summary>
        /// <param name="view"></param>
        private void CreateScheduleManualViewIncidence(ScheduleManualView view)
        {
            try
            {
                var list = new ArrayListDataSet(view)
                {
                    new ScheduleDaysOfWeekFilter(view, Resources.OnceDescription, ScheduleDaysOfWeek.None),
                    new ScheduleDaysOfWeekFilter(view, Resources.EveryMondayDescription, ScheduleDaysOfWeek.Mondays),
                    new ScheduleDaysOfWeekFilter(view, Resources.EveryTuesdayDescription, ScheduleDaysOfWeek.Tuesdays),
                    new ScheduleDaysOfWeekFilter(view, Resources.EveryWednesdayDescrioption, ScheduleDaysOfWeek.Wednesdays),
                    new ScheduleDaysOfWeekFilter(view, Resources.EveryThursdayDescription, ScheduleDaysOfWeek.Thursdays),
                    new ScheduleDaysOfWeekFilter(view, Resources.EveryFridayDescription, ScheduleDaysOfWeek.Fridays),
                    new ScheduleDaysOfWeekFilter(view, Resources.EverySaturdayDescription, ScheduleDaysOfWeek.Saturdays),
                    new ScheduleDaysOfWeekFilter(view, Resources.EverySundayDescription, ScheduleDaysOfWeek.Sundays),
                    new ScheduleDaysOfWeekFilter(view, Resources.WorkingdaysDescription, ScheduleDaysOfWeek.WorkingDays),
                    new ScheduleDaysOfWeekFilter(view, Resources.WeekendsDescription, ScheduleDaysOfWeek.Weekends)
                };
                view.ScheduleIncidence = new Choice(view, null, list);
            }
            catch (Exception exception)
            {
                Logger.Error(string.Format("{0}{1}{2}", "ScheduleManualView", "CreateScheduleManualViewIncidence", exception));
            }
        }

        /// <summary>
        /// CreateScheduleManualViewDates
        /// </summary>
        /// <param name="view"></param>
        private void CreateScheduleManualViewDates(ScheduleManualView view)
        {
            try
            {
                var list = new ArrayListDataSet(view);
                var startingDate = DateTime.Now.Date;
                var endingDate = startingDate.AddMonths(2);
                foreach (var date in GetDateRange(startingDate, endingDate))
                {
                    list.Add(new ModelItem(view, date.ToLongDateString()));
                }
                view.ScheduleDates = new Choice(view, null, list);
            }
            catch (Exception exception)
            {
                Logger.Error(string.Format("{0}{1}{2}", "ScheduleManualView", "CreateScheduleManualViewDates", exception));
            }
        }

        /// <summary>
        /// CreateSchedulePriorityFilters
        /// </summary>
        /// <param name="view"></param>
        private void CreateSchedulePriorityFilters(ScheduleManualView view)
        {
            try
            {
                var list = new ArrayListDataSet(view)
                {
                    new SchedulePriorityFilter(view, Resources.PriorityVeryLowDescription, SchedulePriority.VeryLow),
                    new SchedulePriorityFilter(view, Resources.PriorityLowDescription, SchedulePriority.Low)
                };
                ModelItem normal = new SchedulePriorityFilter(view, Resources.PriorityNormalDescription, SchedulePriority.Normal);
                list.Add(normal);
                list.Add(new SchedulePriorityFilter(view, Resources.PriorityHighDescription, SchedulePriority.High));
                list.Add(new SchedulePriorityFilter(view, Resources.PriorityVeryHighDescription, SchedulePriority.VeryHigh));
                var filters = new Choice(view, null, list) { Chosen = normal };
                view.SchedulePriorityFilters = filters;
            }
            catch (Exception exception)
            {
                Logger.Error(string.Format("{0}{1}{2}", "ScheduleManualView", "CreateSchedulePriorityFilters", exception));
            }
        }

        /// <summary>
        /// CreateScheduleTypeFilters
        /// </summary>
        /// <param name="view"></param>
        private void CreateScheduleTypeFilters(ScheduleManualView view)
        {
            try
            {
                var list = new ArrayListDataSet(view);
                ModelItem recording = new ScheduleTypeFilter(view, Resources.SortByRecordingDescription, ScheduleType.Recording);
                list.Add(recording);
                list.Add(new ScheduleTypeFilter(view, Resources.SortByAlertDescription, ScheduleType.Alert));
                list.Add(new ScheduleTypeFilter(view, Resources.SortBySuggestionDescription, ScheduleType.Suggestion));
                var filters = new Choice(view, null, list) { Chosen = recording };
                view.ScheduleTypeFilters = filters;
            }
            catch (Exception exception)
            {
                Logger.Error(string.Format("{0}{1}{2}", "ScheduleManualView", "CreateScheduleTypeFilters", exception));
            }
        }

        /// <summary>
        /// CreateKeepUntilModeFilters
        /// </summary>
        /// <param name="view"></param>
        private void CreateKeepUntilModeFilters(ScheduleManualView view)
        {
            try
            {
                var list = new ArrayListDataSet(view);
                ModelItem forever = new KeepUntilModeFilter(view, Resources.KeepUntilModeForeverDescription, KeepUntilMode.Forever);
                list.Add(forever);
                list.Add(new KeepUntilModeFilter(view, Resources.KeepUntilModeNumberOfDaysDescription, KeepUntilMode.NumberOfDays));
                list.Add(new KeepUntilModeFilter(view, Resources.KeepUntilModeNumberOfEpisodesDescription, KeepUntilMode.NumberOfEpisodes));
                list.Add(new KeepUntilModeFilter(view, Resources.KeepUntilModeNumberOfWatchedEpisodesDescription, KeepUntilMode.NumberOfWatchedEpisodes));
                list.Add(new KeepUntilModeFilter(view, Resources.KeepUntilModeUntilSpaceIsNeededDescription, KeepUntilMode.UntilSpaceIsNeeded));
                var filters = new Choice(view, null, list) { Chosen = forever };
                view.KeepUntilModeFilters = filters;
            }
            catch (Exception exception)
            {
                Logger.Error(string.Format("{0}{1}{2}", "ScheduleManualView", "CreateKeepUntilModeFilters", exception));
            }
        }

        /// <summary>
        /// Navigate to ScheduleManualView
        /// </summary>
        /// <param name="scheduleManualView"></param>
        private void GoToScheduleManualView(ScheduleManualView scheduleManualView)
        {
            try
            {
                var uiProperties = new Dictionary<string, object>();
                uiProperties["ScheduleManualView"] = scheduleManualView;
                _session.GoToPage("resx://ArgusTV.UI.MediaCenter/ArgusTV.UI.MediaCenter.Resources/ScheduleManualView", uiProperties);
            }
            catch (Exception exception)
            {
                Logger.Error(string.Format("{0}{1}{2}", "ScheduleManualView", "GoToScheduleManualView",exception));
            }
        }

        #endregion
    }
}